import java.net.DatagramPacket;
import java.net.InetAddress;
import java.util.StringTokenizer;


public class MyConnectionSocket implements Runnable {
	// create new ConnectionSocket socket object
    private MyConnectionSocket socket;

    // overload constructor
    public MyConnectionSocket(MyConnectionSocket obj)
    {
        this.socket = obj;
    }

    // start server thread
    public void run()
    {
        int job;
        String temp = "";
        Packet data = new Packet();

        while(true)
        {
            try
            {
                Packet msg = socket.receive();
                job = msg.getJob();
                
                Packet sendMe = new Packet(msg.getIPofSender(), msg.getIPofDestination(), msg.getPort(), "", msg.getJob());

                // cast message as String to set data in new connections
                temp = (String)msg.getMessage();

                if(job == 1)
                {
                    sendMe.setMessage(removeAllVowels(temp));
                }
                else if(job == 2)
                {
                    sendMe.setMessage(removeAllSpaces(temp));
                }
                else if(job == 3)
                {
                    sendMe.setMessage(makeAllCaps(temp));
                }
                send(sendMe);
            }
            catch (Exception er){}
        }
    }

    // used for sending message, message length, ip address and port number
    private void send(Packet msg)
    {
        //this should push to a queue
    }
    
    private Packet receive(){
    		//pull from a queue
    	
    }

    /*
        Server functionalities
        removeAllSpaces() - remove all spaces from given sentence
        removeAllVowels() - remove all vowels from given sentence
        make all caps - capitalize given sentence
    */

    // This removes all spaces from a string sentence
    public String removeAllSpaces(String sentence)
    {
        StringTokenizer line = new StringTokenizer(sentence, " ");
        StringBuffer removeSpaces = new StringBuffer("");
        while (line.hasMoreTokens())
        {
            removeSpaces.append(line.nextToken());
        }
        return removeSpaces.toString();
    }

    // This removes all vowels from a string sentence
    public String removeAllVowels(String sentence)
    {
        int sentenceLength = 0;
        String removeVowels = "";
        String vowels = "[AaEeIiOoUu]";

        sentenceLength = sentence.length();
        for(int i=0; i < sentenceLength; i++)
        {
            if(sentence.matches(vowels));
            {
                removeVowels = sentence.replaceAll(vowels, "");
            }
        }
        return removeVowels;
    }

    // This converts a string sentence into upper case
    public String makeAllCaps(String sentence)
    {
        return sentence.toUpperCase();
    }

        
}

